package com.property.demo.util;

import com.property.demo.dto.*;
import com.property.demo.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.property.demo.service.OwnerService;

import java.util.List;
import java.util.stream.Collectors;

@Component
public class DTOMapper {

    private static OwnerService ownerService;
    
    @Autowired
    public DTOMapper(OwnerService ownerService) {
        DTOMapper.ownerService = ownerService;
    }

    public static OwnerDTO toOwnerDTO(Owner owner) {
        if (owner == null) {
            return null;
        }

        OwnerDTO dto = new OwnerDTO();
        dto.setId(owner.getId());
        dto.setName(owner.getName());
        dto.setGender(owner.getGender());
        dto.setPhoneNumber(owner.getPhoneNumber());
        dto.setIdCard(owner.getIdCard());
        dto.setProfession(owner.getProfession());
        dto.setEmail(owner.getEmail());
        dto.setRemarks(owner.getRemarks());
        
        // 通过服务获取小区信息
        if (ownerService != null) {
            String communityName = ownerService.findCommunityNameByOwnerId(owner.getId());
            if (communityName != null) {
                dto.setCommunityName(communityName);
                // 暂不设置communityId，因为我们只能获取到名称
            }
        }
        
        dto.setCreatedAt(owner.getCreatedAt());
        dto.setUpdatedAt(owner.getUpdatedAt());
        
        return dto;
    }

    public static VehicleDTO toVehicleDTO(Vehicle vehicle) {
        if (vehicle == null) {
            return null;
        }

        VehicleDTO dto = new VehicleDTO();
        dto.setId(vehicle.getId());
        dto.setLicensePlate(vehicle.getLicensePlate());
        dto.setPlateNumber(vehicle.getPlateNumber());
        dto.setBrand(vehicle.getBrand());
        dto.setModel(vehicle.getModel());
        dto.setColor(vehicle.getColor());
        dto.setType(vehicle.getType());
        dto.setOwner(toOwnerDTO(vehicle.getOwner()));
        dto.setParkingLocation(vehicle.getParkingLocation());
        dto.setParkingPermitNumber(vehicle.getParkingPermitNumber());
        dto.setPermitExpiryDate(vehicle.getPermitExpiryDate());
        dto.setStatus(vehicle.getStatus());
        dto.setRemarks(vehicle.getRemarks());
        dto.setCreatedAt(vehicle.getCreatedAt());
        dto.setUpdatedAt(vehicle.getUpdatedAt());
        
        return dto;
    }
    
    public static HomeDTO toHomeDTO(Home home) {
        if (home == null) {
            return null;
        }

        HomeDTO dto = new HomeDTO();
        dto.setId(home.getId());
        dto.setName(home.getName());
        dto.setFloor(home.getFloor());
        dto.setArea(home.getArea());
        dto.setType(home.getType());
        dto.setDirection(home.getDirection());
        dto.setStatus(home.getStatus());
        
        if (home.getBuilding() != null) {
            dto.setBuildingId(home.getBuilding().getId());
            dto.setBuildingName(home.getBuilding().getName());
        }
        
        if (home.getOwner() != null) {
            dto.setOwnerId(home.getOwner().getId());
            dto.setOwnerName(home.getOwner().getName());
        }
        
        dto.setCreatedAt(home.getCreatedAt());
        dto.setUpdatedAt(home.getUpdatedAt());
        
        return dto;
    }
    
    public static ParkingUsageDTO toParkingUsageDTO(ParkingUsage parkingUsage) {
        if (parkingUsage == null) {
            return null;
        }

        ParkingUsageDTO dto = new ParkingUsageDTO();
        dto.setId(parkingUsage.getId());
        
        if (parkingUsage.getParking() != null) {
            dto.setParkingId(parkingUsage.getParking().getId());
            // Use the parking name as code since code doesn't exist
            dto.setParkingCode(parkingUsage.getParking().getName()); 
            dto.setParkingName(parkingUsage.getParking().getName());
            
            if (parkingUsage.getParking().getCommunity() != null) {
                dto.setCommunityName(parkingUsage.getParking().getCommunity().getName());
            }
        }
        
        if (parkingUsage.getOwner() != null) {
            dto.setOwnerId(parkingUsage.getOwner().getId());
            dto.setOwnerName(parkingUsage.getOwner().getName());
            dto.setOwnerPhone(parkingUsage.getOwner().getPhoneNumber());
        }
        
        if (parkingUsage.getVehicle() != null) {
            dto.setVehicleId(parkingUsage.getVehicle().getId());
            dto.setCarNumber(parkingUsage.getVehicle().getLicensePlate());
        }
        
        // 将整数类型的usageType转换为字符串
        if (parkingUsage.getUsageType() != null) {
            switch (parkingUsage.getUsageType()) {
                case 1:
                    dto.setUsageType("租用");
                    break;
                case 2:
                    dto.setUsageType("购买");
                    break;
                case 3:
                    dto.setUsageType("临时");
                    break;
                default:
                    dto.setUsageType("未知");
            }
        }
        
        dto.setStartDate(parkingUsage.getStartDate());
        dto.setEndDate(parkingUsage.getEndDate());
        dto.setFee(parkingUsage.getFee());
        dto.setPaymentStatus(parkingUsage.getPaymentStatus());
        dto.setRemarks(parkingUsage.getRemarks());
        dto.setCreatedAt(parkingUsage.getCreatedAt());
        dto.setUpdatedAt(parkingUsage.getUpdatedAt());
        
        return dto;
    }
    
    public static ComplaintDTO toComplaintDTO(Complaint complaint) {
        if (complaint == null) {
            return null;
        }
        
        ComplaintDTO dto = new ComplaintDTO();
        dto.setId(complaint.getId());
        
        if (complaint.getOwner() != null) {
            dto.setOwnerId(complaint.getOwner().getId());
            dto.setOwnerName(complaint.getOwner().getName());
            dto.setOwnerPhone(complaint.getOwner().getPhoneNumber());
            
            // 通过业主ID查询所属小区
            if (ownerService != null) {
                String communityName = ownerService.findCommunityNameByOwnerId(complaint.getOwner().getId());
                dto.setCommunityName(communityName);
            }
        }
        
        dto.setTitle(complaint.getTitle());
        dto.setContent(complaint.getContent());
        dto.setType(complaint.getType());
        dto.setStatus(complaint.getStatus());
        dto.setSubmitTime(complaint.getSubmitTime());
        dto.setHandleTime(complaint.getHandleTime());
        dto.setFinishTime(complaint.getFinishTime());
        dto.setHandlerName(complaint.getHandlerName());
        dto.setResult(complaint.getResult());
        dto.setCreatedAt(complaint.getCreatedAt());
        dto.setUpdatedAt(complaint.getUpdatedAt());
        
        return dto;
    }

    public static List<VehicleDTO> toVehicleDTOList(List<Vehicle> vehicles) {
        return vehicles.stream()
                .map(DTOMapper::toVehicleDTO)
                .collect(Collectors.toList());
    }

    public static List<OwnerDTO> toOwnerDTOList(List<Owner> owners) {
        return owners.stream()
                .map(DTOMapper::toOwnerDTO)
                .collect(Collectors.toList());
    }
    
    public static List<ParkingUsageDTO> toParkingUsageDTOList(List<ParkingUsage> parkingUsages) {
        return parkingUsages.stream()
                .map(DTOMapper::toParkingUsageDTO)
                .collect(Collectors.toList());
    }
    
    public static List<ComplaintDTO> toComplaintDTOList(List<Complaint> complaints) {
        return complaints.stream()
                .map(DTOMapper::toComplaintDTO)
                .collect(Collectors.toList());
    }
    
    public static List<HomeDTO> toHomeDTOList(List<Home> homes) {
        return homes.stream()
                .map(DTOMapper::toHomeDTO)
                .collect(Collectors.toList());
    }

    public static MailboxDTO toMailboxDTO(Mailbox mailbox) {
        if (mailbox == null) {
            return null;
        }
        
        MailboxDTO dto = new MailboxDTO();
        dto.setId(mailbox.getId());
        
        if (mailbox.getOwner() != null) {
            dto.setOwnerId(mailbox.getOwner().getId());
            dto.setOwnerName(mailbox.getOwner().getName());
            dto.setOwnerPhone(mailbox.getOwner().getPhoneNumber());
            
            // 通过业主ID查询所属小区
            if (ownerService != null) {
                String communityName = ownerService.findCommunityNameByOwnerId(mailbox.getOwner().getId());
                dto.setCommunityName(communityName);
            }
        }
        
        dto.setTitle(mailbox.getTitle());
        dto.setContent(mailbox.getContent());
        dto.setType(mailbox.getType());
        dto.setStatus(mailbox.getStatus());
        dto.setSubmitTime(mailbox.getSubmitTime());
        dto.setReplyTime(mailbox.getReplyTime());
        dto.setReplyContent(mailbox.getReplyContent());
        dto.setHandlerName(mailbox.getHandlerName());
        dto.setCreatedAt(mailbox.getCreatedAt());
        dto.setUpdatedAt(mailbox.getUpdatedAt());
        
        return dto;
    }
    
    public static List<MailboxDTO> toMailboxDTOList(List<Mailbox> mailboxes) {
        return mailboxes.stream()
                .map(DTOMapper::toMailboxDTO)
                .collect(Collectors.toList());
    }
} 